package com.cmfi.controller;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cmfi.domain.userRoleAuthEntity.User;
import com.cmfi.service.inter.AndonService;
import com.cmfi.utils.Code;
import com.cmfi.utils.Result;

import lombok.extern.slf4j.Slf4j;

//return都是result

@Slf4j
@RestController
@RequestMapping("/api/andon")
public class AndonController {
    @Autowired
    private AndonService andonService;
    static Logger log = LoggerFactory.getLogger(AndonController.class);
    
    @PostMapping("/monitorandon")
    @PreAuthorize("hasAuthority('anDon')")
    public Result monitorAndon() {
    	log.info("[AndonController][monitorAndon] monitoring.");
    	try {
    		andonService.save();
    	    return new Result(Code.SAVE_OK, "Monitor_OK");
    	} catch (Exception e) {
    		log.info("REST request to save e,", e);
    		return new Result(Code.SAVE_ERR, e.getMessage());
    	}
    }
    
    //这个接口的返回值要在大屏上显示，问下徐工这个怎么做
    @PostMapping("/linesqualityequipmentandcallings")
    @PreAuthorize("hasAuthority('anDon')")
    public Result linesQualityEquipmentAndCallings(@RequestParam Map<String, Object> params) {
    	log.info("[AndonController][linesQualityEquipmentAndCallings] parameters:{}", params);
    	Integer actionId = 0;
		String roleId = null;
    	try {
			if (params == null) {
				return new Result(null, Code.SAVE_ERR, "入参为空", roleId);
			}
			if (params.containsKey("actionId")) {
				actionId = Integer.parseInt(params.get("actionId").toString());
			}
			if (params.containsKey("roleId")) {
				roleId = params.get("roleId").toString();
			}
			if (actionId == 1) {
				andonService.stopplingLines();
				return new Result(null, Code.SAVE_OK, "Stop_OK", roleId);
			}else if(actionId == 2) {
				return new Result(null, Code.SAVE_OK, "Quality_Problems", roleId);
			}else if(actionId == 3) {
				return new Result(null, Code.SAVE_OK, "Equipment_Problems", roleId);
			}else if(actionId == 4) {
				User calling = new User();
				calling = andonService.findCallings();
				return new Result(calling, Code.SAVE_OK, "Callings", roleId);
			}else {
				return new Result(null, Code.SAVE_ERR, "动作ID错误", roleId);
			}
		} catch (Exception e) {
			log.error("[AndonController][linesQualityEquipmentAndCallings] e:{}", e);
			return new Result(null, Code.SAVE_ERR, e.getMessage(), roleId);
    }	
  }
}

